package com.hualan.controller;

import com.github.pagehelper.PageInfo;
import com.hualan.pojo.Feedback;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Questions;
import com.hualan.pojo.Result;
import com.hualan.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;


    //查询所有问题
    @GetMapping("/all")
    public Result getAllFeedbcaks(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageResult<Feedback> result = feedbackService.getAllFeedback(page, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("系统繁忙，请稍后再试");
        }
    }

    @GetMapping("/selectByQuesId")
    public Result getFeedbackByQuesId(
            @RequestParam Integer quesId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        try {
            PageInfo<Feedback> pageInfo = feedbackService.getFeedbackWithConsultantByQuesId(quesId, pageNum, pageSize);
            return Result.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result deleteFeedback(@PathVariable Long id) {
        try {
            int result = feedbackService.deleteByPrimaryKey(id);
            if (result > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("未找到对应记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }

    //批量删除问题
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return Result.error("参数错误：ID 列表不能为空");
            }
            int result = feedbackService.batchDelete(ids);
            return Result.success("成功删除 " + result + " 条记录");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }




    //插入回复
    @PostMapping("/add")
    public Result  addFeedback(@RequestBody Feedback feedback) {
        try {
            int result = feedbackService.addFeedback(feedback);
            if (result > 0) {
                return Result.success("回复成功");
            } else {
                return Result.error("回复失败");
            }
        } catch (Exception e) {
            return Result.error("服务器错误: " + e.getMessage());
        }
    }

    //根据content模糊查询
    @GetMapping("/search")
    public Result searchByContent(
            @RequestParam String content,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageResult<Feedback> result = feedbackService.searchByContent(content, page, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统繁忙，请稍后再试");
        }
    }

}
